$(function () {
    // initMenu();
})
/**
 * 初始化菜单列表
 */
function initMenu() {
    $.ajax({
        type:"get",
        url:"/admin/menu/initMenu2?ran="+Math.random(),
        // url:"/admin/menu/initMenu?ran="+Math.random(),
        data:{},
        dataType:"json",
        success:function (msg) {
            if(msg != "false"){
                var menu = "<ul>";
                //添加一级菜单
                for(var i=0; i< msg.length;i++){
                    var bean = msg[i];
                    if("0"==bean.menuPid){
                        var li = "<li class=\"menu-title\" id='ul"+bean.id+"'><a href=\"javascript:void(0);\">" +
                            "</a>"+bean.menuName+"<span class=\"menu-arrow\"></span></li>";
                        // var li = "<li class=\"menu-title\"><a href=\"javascript:void(0);\"></a>"+bean.menuName+"<span class=\"menu-arrow\"></span>" +
                        //     "<ul  class=\"list-unstyled\" id='ul"+bean.id+"'></ul></li>";
                        menu += li;
                    }
                }
                menu = menu + "</ul><div class=\"clearfix\"></div>";
                $("#sidebar-menu").append(menu);

                //添加二级菜单
                for(var i=0;i<msg.length;i++){
                    var bean = msg[i];
                    if(1<=bean.menuPid<=4){
                        var li = "<li class='has_sub'><a href=\"javascript:void(0);\" id='liul"+bean.id+"' class=\"waves-effect waves-primary\">"
                            +"<i class='"+bean.icon+"'></i><span>"+bean.menuName+"</span> <span class=\"menu-arrow\"></span></a>" +
                            "<ul class=\"list-unstyled\" id='ul"+bean.id+"'></ul></li>";
                        var parentId = "#ul" + bean.menuPid;
                        $(parentId).after(li);
                    }
                }

                //添加三级级菜单
                for(var i=0; i<msg.length;i++) {
                    var bean = msg[i];
                    if (6<= bean.menuPid) {
                        var url = bean.url;
                        var ids = "";
                        if (url) {
                            ids = url.substr(url.lastIndexOf("/") + 1);
                        }
                        if (!url) {
                            url = "#";
                        }
                        var li = "<li href='javascript:void(0);' onclick=\"locationUrl('" + url + "','" + ids + "','1')\" >" + bean.menuName + "</li>";
                        var li = "<li><a href='" + url + "' >" + bean.menuName + "</a></li>";
                        var parentId = "#ul" + bean.menuPid;
                        $(parentId).append(li);
                    }
                }

            }else {
                alert("初始化菜单失败！！！");
            }
        },
        error:function () {
            alert("初始化订单异常！！！");
        }
    })
}


/**
 * 执行删除操作
 * @param method
 * @param url
 * @param param
 */
function H5DelData(method,url, param,reload) {
    $.ajax({
        type :method,
        url  : url,
        data : param,
        async:false,
        dataType : "json",
        success : function(res){
            var msg;
            if(res.success=="true"){
                swal("系统提示",res.msg, "success");
                // messageOk("操作成功");
            }
            else{
                swal("系统提示",res.msg,"error");
                // messageWarn("操作失败");
            }
        },
        timeout: 10000,
        error: function(XMLHttpRequest, textStatus, errorThrown){
            swal("系统提示",10000, "error");
            // messageError("操作异常");
        }
    });
}

/**
 * YsRequset
 * @param type
 * @param url
 * @param param
 * @param param
 */
function H5Requset(url,param,sendUrl) {
    var result = "";
    var loadi=layer.load("正在执行操作，请稍后....");
    $.ajax({
        type :"get",
        url  : url,
        data : param,
        dataType : "json",
        async:false,
        success : function(res){
            console.log(res);
            layer.close(loadi);
            if(res.success=="true"){
                messageOk("操作成功");
                if(sendUrl){
                    fvTable.ajax.url(sendUrl+"?ran="+Math.random()).load();
                }
                result =  "1";
            }else{
                // messageWarn("操作失败");
                messageWarn(res.msg);
                result = "2";
            }
        },
        timeout: 10000,
        error: function(XMLHttpRequest, textStatus, errorThrown){
            layer.close(loadi);
            // swal("系统提示","操作异常，请稍后再试","error");
            messageError("操作异常，请稍后再试");
            result = "2";
        }
    });
    return result;
}

/**
 * 显示提示信息(用于表单验证提示)
 * @param msg
 */
function M_showMessageOK(msg){
    swal({
        title: "系统提示",
        text: msg,
        type: "success",
        showCancelButton: false,
        confirmButtonClass: 'btn-danger waves-effect waves-light',
        confirmButtonText: '确定'
    });
}

/**
 * 显示提示信息(用于表单验证提示)
 * @param msg
 */
function M_showMessageWarn(msg){
    swal({
        title: "系统提示",
        text: msg,
        type: "warning",
        showCancelButton: false,
        confirmButtonClass: 'btn-danger waves-effect waves-light',
        confirmButtonText: '确定'
    });
}

/**
 * 显示提示信息(用于表单验证提示)
 * @param msg
 */
function M_showMessageError(msg){

    swal({
        title: "系统提示",
        text: msg,
        type: "error",
        showCancelButton: false,
        confirmButtonClass: 'btn-danger waves-effect waves-light',
        confirmButtonText: '确定'
    });
}

//showResponse
function showResponse(data,url){
    layer.close(loadi);
    if(data.success=="true"){
        swal({
                title:"系统提示",
                text: data.msg,
                type: "success",
                showCancelButton: false,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "关闭窗口",
                cancelButtonText: "返回列表",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function(isConfirm){
                location.href=url;
            });
    }
    else{
        swal("系统提示",data.msg, "error");
    }
}


//(修改密码)
function updateLoginPwd() {

    var source=$("#push-login-template").html();
    var template=Handlebars.compile(source);
    var context={};
    var result=template(context);
    bootbox.dialog({
        title:"<i class='fa fa-key text-success'> </i>&nbsp;&nbsp;<span style='font-size: 15px'>修改密码</span>",
        message: result,
        buttons: {
            success: {
                label: "<i class='fa fa-check'></i>&nbsp;确认修改",
                className: 'btn-success btn-md',
                callback:function () {
                    var id = $("#userId").val();
                    var new_password = $("#new_password").val();
                    var sure_password = $("#sure_password").val();
                    if(new_password==""){
                        messageError("新密码不能为空");
                        return;
                    }else if(sure_password==""){
                        messageError("确认密码不能为空");
                        return;
                    }else if (new_password != sure_password){
                        messageError("密码不一致,请检查！");
                        return;
                    }
                    var param={"id":id,"passWord":sure_password};
                    H5Requset("/hc/adminUser/updateUserById",param,"/hc/adminUser/initUserList");
                }
            },
            Cancel:{
                label: "<i class='fa fa-times'></i>&nbsp;关闭窗口",
                className: 'btn-danger btn-md'
            }
        }
    });
}


/**
 * 提示框
 **/
function messageOk(content){
    $.Notification.notify("success","top center","温馨提示",content);
}

function messageWarn(content){
    $.Notification.notify("warning","top center","温馨提示",content);
}

function messageError(content){
    $.Notification.notify("error","top center","温馨提示",content);
}